<?php

/**
 * cc_question actions.
 *
 * @package    sf_sandbox
 * @subpackage cc_question
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */

class cc_questionActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('cc_question', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/cc_question/filters');

    // pager
    $this->pager = new sfPropelPager('CcQuestion', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/cc_question')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/cc_question');
    }
  }

  public function executeCreate()
  {
    return $this->forward('cc_question', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('cc_question', 'edit');
  }

  public function executeEdit()
  {
    $this->cc_question = $this->getCcQuestionOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateCcQuestionFromRequest();

      $this->saveCcQuestion($this->cc_question);

      $this->setFlash('notice', 'Thông tin đã được cập nhật');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('cc_question/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('cc_question/list');
      }
      else
      {
        return $this->redirect('cc_question/edit?id='.$this->cc_question->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->cc_question = CcQuestionPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->cc_question);

    try
    {
      $this->deleteCcQuestion($this->cc_question);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Cc question. Make sure it does not have any associated items.');
      return $this->forward('cc_question', 'list');
    }

    return $this->redirect('cc_question/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->cc_question = $this->getCcQuestionOrCreate();
    $this->updateCcQuestionFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveCcQuestion($cc_question)
  {
    $cc_question->save();

  }

  protected function deleteCcQuestion($cc_question)
  {
    $cc_question->delete();
  }

  protected function updateCcQuestionFromRequest()
  {
    $cc_question = $this->getRequestParameter('cc_question');

    if (isset($cc_question['title']))
    {
      $this->cc_question->setTitle($cc_question['title']);
    }
    if (isset($cc_question['content']))
    {
      $this->cc_question->setContent($cc_question['content']);
    }
    if (isset($cc_question['response']))
    {
      $this->cc_question->setResponse($cc_question['response']);
    }
    if (isset($cc_question['phone']))
    {
      $this->cc_question->setPhone($cc_question['phone']);
    }
    if (isset($cc_question['cc_name']))
    {
      $this->cc_question->setCcName($cc_question['cc_name']);
    }
    if (isset($cc_question['created_datetime']))
    {
      if ($cc_question['created_datetime'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($cc_question['created_datetime']))
          {
            $value = $dateFormat->format($cc_question['created_datetime'], 'I', $dateFormat->getInputPattern('g'));
          }
          else
          {
            $value_array = $cc_question['created_datetime'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->cc_question->setCreatedDatetime($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->cc_question->setCreatedDatetime(null);
      }
    }
  }

  protected function getCcQuestionOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $cc_question = new CcQuestion();
    }
    else
    {
      $cc_question = CcQuestionPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($cc_question);
    }

    return $cc_question;
  }

  protected function processFilters()
  {
    if ($this->getRequest()->hasParameter('filter'))
    {
      $filters = $this->getRequestParameter('filters');
      if (isset($filters['created_datetime']['from']) && $filters['created_datetime']['from'] !== '')
      {
        $filters['created_datetime']['from'] = sfI18N::getTimestampForCulture($filters['created_datetime']['from'], $this->getUser()->getCulture());
      }
      if (isset($filters['created_datetime']['to']) && $filters['created_datetime']['to'] !== '')
      {
        $filters['created_datetime']['to'] = sfI18N::getTimestampForCulture($filters['created_datetime']['to'], $this->getUser()->getCulture());
      }

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/cc_question');
      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/cc_question/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/cc_question/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/cc_question/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/cc_question/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/cc_question/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['id_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::ID, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::ID, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['id']) && $this->filters['id'] !== '')
    {
      $c->add(CcQuestionPeer::ID, $this->filters['id']);
    }
    if (isset($this->filters['title_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::TITLE, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::TITLE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['title']) && $this->filters['title'] !== '')
    {
      $c->add(CcQuestionPeer::TITLE, strtr($this->filters['title'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['content_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::CONTENT, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::CONTENT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['content']) && $this->filters['content'] !== '')
    {
      $c->add(CcQuestionPeer::CONTENT, strtr($this->filters['content'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['response_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::RESPONSE, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::RESPONSE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['response']) && $this->filters['response'] !== '')
    {
      $c->add(CcQuestionPeer::RESPONSE, strtr($this->filters['response'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['phone_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::PHONE, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::PHONE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['phone']) && $this->filters['phone'] !== '')
    {
      $c->add(CcQuestionPeer::PHONE, strtr($this->filters['phone'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['cc_name_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::CC_NAME, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::CC_NAME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['cc_name']) && $this->filters['cc_name'] !== '')
    {
      $c->add(CcQuestionPeer::CC_NAME, strtr($this->filters['cc_name'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['created_datetime_is_empty']))
    {
      $criterion = $c->getNewCriterion(CcQuestionPeer::CREATED_DATETIME, '');
      $criterion->addOr($c->getNewCriterion(CcQuestionPeer::CREATED_DATETIME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_datetime']))
    {
      if (isset($this->filters['created_datetime']['from']) && $this->filters['created_datetime']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(CcQuestionPeer::CREATED_DATETIME, $this->filters['created_datetime']['from'], Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_datetime']['to']) && $this->filters['created_datetime']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(CcQuestionPeer::CREATED_DATETIME, $this->filters['created_datetime']['to'], Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(CcQuestionPeer::CREATED_DATETIME, $this->filters['created_datetime']['to'], Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/cc_question/sort'))
    {
      $sort_column = CcQuestionPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/cc_question/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'cc_question{id}' => 'ID:',
      'cc_question{title}' => 'Tiêu đề:',
      'cc_question{content}' => 'Nội dung:',
      'cc_question{response}' => 'Trả lời:',
      'cc_question{phone}' => 'Phone KH:',
      'cc_question{cc_name}' => 'Tên CC:',
      'cc_question{created_datetime}' => 'Thời gian:',
    );
  }
}

